package com.hzcx.app.simplechat.ui.setting;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.ui.setting.adapter.BlackListAdapter;
import com.hzcx.app.simplechat.ui.setting.bean.BlackListBean;
import com.hzcx.app.simplechat.ui.setting.contract.BlackListContract;
import com.hzcx.app.simplechat.ui.setting.presenter.BlackListPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 黑名单列表
 */
public class BlackListActivity extends BaseActivity<BlackListPresenter> implements BaseQuickAdapter.OnItemChildClickListener, BlackListContract.View {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.rv_black_list)
    RecyclerView rvBlackList;
    @BindView(R.id.ll_null)
    LinearLayout llNull;

    private List<BlackListBean> data;
    private BlackListAdapter blackListAdapter;

    @Override
    public int getLayout() {
        return R.layout.activity_black_list;
    }

    @Override
    public void initPresenter() {
        mPresenter = new BlackListPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText("黑名单");

        data = new ArrayList<>();
        blackListAdapter = new BlackListAdapter(R.layout.rv_item_black_list, data);
        rvBlackList.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvBlackList.setAdapter(blackListAdapter);
    }

    @Override
    public void initData() {

        blackListAdapter.setOnItemChildClickListener(this);

        mPresenter.getBlackList(this);
    }

    /**
     * 子控件点击
     *
     * @param adapter
     * @param view
     * @param position
     */
    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()) {
            case R.id.ll_delete: // 移除黑名单
                deleteBlack(position);
                break;
        }
    }

    private void deleteBlack(int pos) {
        showConfirmDialog(null, "确定要移除黑名单吗？", null, () ->
                mPresenter.putUserOutBlack(BlackListActivity.this, data.get(pos).getMember_id(), pos));
    }

    /**
     * 黑名单列表获取成功
     *
     * @param data
     */
    @Override
    public void blackListResult(List<BlackListBean> data) {
        if (data == null || data.size() <= 0) {
            return;
        }
        this.data.addAll(data);
        blackListAdapter.notifyDataSetChanged();
        if (this.data == null || this.data.size() <= 0) {
            llNull.setVisibility(View.VISIBLE);
        } else {
            llNull.setVisibility(View.GONE);
        }
    }

    /**
     * 黑名单移除成功
     */
    @Override
    public void outBlackSuccess(int pos) {
        data.remove(pos);
        blackListAdapter.notifyDataSetChanged();
        showError("移除黑名单成功");
        if (this.data == null || this.data.size() <= 0) {
            llNull.setVisibility(View.VISIBLE);
        } else {
            llNull.setVisibility(View.GONE);
        }
    }

}
